package main.firefox;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.TreeMap;

public class SortReaderObj {

	private TreeMap<Integer, SortObj> mapResults = new TreeMap<Integer, SortObj>();

	public SortReaderObj(String filePath) {

		File file = new File(filePath);
		readMapFromCSV(file);

	}

	public void readMapFromCSV(File file) {

		mapResults.clear();
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";

		SortObj deo;

		try {

			br = new BufferedReader(new FileReader(file));

			boolean firstTime = true;

			while ((line = br.readLine()) != null) {

				String[] values = line.split(cvsSplitBy);

				if (!firstTime) {

					if (mapResults.get(Integer.parseInt(values[0].trim())) == null) {
						deo = new SortObj();
						deo.setBugID(Integer.parseInt(values[0].trim()));

						if (values[1] != null && values[1].trim().length() > 0) {
							try {
								deo.setDate(new SimpleDateFormat(
										"yyyy-MM-dd HH:mm:ss").parse(values[1]));
							} catch (Throwable t) {
								// System.out.println("Error parsing date at "+values[1]);
							}
						}

						if (values.length > 2) {

							if (values[2] != null
									&& values[2].trim().length() > 0) {
								try {
									deo.setResolutionsDate(new SimpleDateFormat(
											"yyyy-MM-dd HH:mm:ss")
											.parse(values[2]));
								} catch (Throwable t) {
									// System.out.println("Error parsing date at "+values[2]
									// );
								}
							}
						}

						if (values.length > 2) {

							try {
								deo.setAnyResoDate(new SimpleDateFormat(
										"yyyy-MM-dd HH:mm:ss").parse(values[6]));

							} catch (Throwable t) {
								// System.out.println("Error parsing date at "+values[6]
								// );
							}
							// }

						}
					} else {
						deo = mapResults
								.get(Integer.parseInt(values[0].trim()));
					}

					mapResults.put(deo.getBugID(), deo);

				}

				firstTime = false;
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public TreeMap<Integer, SortObj> getMapResults() {
		return mapResults;
	}

	public void setMapResults(TreeMap<Integer, SortObj> mapResults) {
		this.mapResults = mapResults;
	}

}
